﻿using Edu.Abstracts;
using Edu.EntityFrameworkCore;
using Edu.Entitys;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace Edu.Repositorys
{
    public class EfCoreAppUserRepository : EfCoreRepository<EduDbContext, AppUser, Guid>, IAppUserRepository
    {

        public EfCoreAppUserRepository(IDbContextProvider<EduDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }


        public virtual async Task<AppUser> FindByPhoneNumberAsync(string phoneNumber, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .FirstOrDefaultAsync(
                    u => u.PhoneNumber == phoneNumber,
                    GetCancellationToken(cancellationToken)
                );
        }

        public virtual async Task<AppUser> FindByUserNameAsync(string userName, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .FirstOrDefaultAsync(
                    u => u.UserName == userName,
                    GetCancellationToken(cancellationToken)
                );
        }

        public virtual async Task<List<AppUser>> GetListByArea(string Area, string sorting = null,
            int maxResultCount = int.MaxValue,
            int skipCount = 0, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .Where(u => u.Area == Area)
                .OrderBy(sorting ?? nameof(AppUser.UserName))
                .PageBy(skipCount, maxResultCount)
                .ToListAsync(GetCancellationToken(cancellationToken));
        }
    }
}
